<?php
define("APP", true);

require_once('config.php');
require_once('session.php');
require_once('database.php');
require_once('template.php');
include ('languages/'.DEFAULT_LANG.'.php');

$session  = new Session();
$template = new Template();
$database = new Database();

function get_score($member_id, $gametype)
{
	global $database, $session;
	if($session->get("member_score") == NULL)
	{
		$database->set_query("SELECT passed FROM results WHERE member_id = " . $member_id . " AND question_type_id = " . $gametype);
		$session->set("member_score_game".$gametype, $database->get_result());
	}
	return $session->get("member_score_game".$gametype);
}

function get_top_players($gametype = 0)
{
	global $database;
	$database->set_query("SELECT username, fullname, date_done 
						FROM results 
						INNER JOIN member 
							ON member.id = results.member_id 
						".($gametype != 0 ? "
						WHERE question_type_id = $gametype
						" : "") . "
						GROUP BY member.id
						ORDER BY MAX( date_done ) DESC
						LIMIT " . MAX_LIST);
	$players = $database->get_object_list();
	
	$k = 1;
	$top_members = array();
	foreach($players as $player)
	{
		$top_members[$k]['id']	  = $k;
		$top_members[$k]['name']  = !empty($player->fullname) ? $player->fullname : $player->username;
		$top_members[$k]['date'] = date("d/m/Y h:i A", strtotime($player->date_done));
		$k++;
	}
	return $top_members;
}

function check_login($member_id)
{
	global $template;
	if(!$member_id)
	{
		$template->assign("message",_MSG_LOGIN_TO_PLAY_GAME);
		$template->display("nologin.tpl");die();
	}
}

function check_finish_game($member_id, $gametype)
{
	global $template, $database, $session;
	if(get_score($member_id, $gametype) != NULL)
	{
		$title = "";
		$message = "";
		if($gametype == GAME1) {
			$logo = _LB_GET_THE_PICTURE_LOGO;
			$title = _LB_GET_THE_PICTURE;
			$message = _MSG_GAME_FINISHED;
		}
		if($gametype == GAME2) {
			$logo = _LB_CULTURAL_MOSAIC_LOGO;
			$title = _LB_CULTURAL_MOSAIC;
			$message = _MSG_GAME_FINISHED;
		}
		if($gametype == GAME3) {
			$logo = _LB_TET_TET_TET_LOGO;
			$title = _LB_TET_TET_TET;
			$message = _MSG_GAME_FINISHED;
		}
		$template->assign("logo", $logo);
		$template->assign("title", $title);
		$template->assign("message", $message);
		$template->display("nologin.tpl");die();
	}
	else
	{
		$is_playing = $session->get('current') ? $session->get('current') : 0;
		if(!$is_playing)
		{
			$database->query("	DELETE member_answer_question
								FROM member_answer_question
								INNER JOIN question
									ON question.id = member_answer_question.question_id
								WHERE member_id = $member_id AND question.question_type_id = $gametype");
		}
	}
}

function check_no_questions($gametype, $max_question)
{
	global $template, $database;
	$database->set_query("SELECT COUNT(id) 
							FROM question 
							WHERE question_type_id = $gametype");
	$num_questions = $database->get_result();
	if($num_questions < $max_question)
	{
		if ($gametype == GAME1) {
			$template->assign("title", _LB_GET_THE_PICTURE);
			$template->assign("logo", _LB_GET_THE_PICTURE_LOGO);
		}
		elseif ($gametype == GAME2) {
			$template->assign("title", _LB_CULTURAL_MOSAIC);
			$template->assign("logo", _LB_CULTURAL_MOSAIC_LOGO);
		}
		elseif ($gametype == GAME3) {
			$template->assign("title", _LB_TET_TET_TET);
			$template->assign("logo", _LB_TET_TET_TET_LOGO);
		}
		$template->assign("message", _MSG_SELECT_ANOTHER_GAME);
		$template->display("nologin.tpl");die();
	}
}

function get_random_questions($gametype, $member_id, $max_question)
{
	global $database, $session;
	$arr_questions	= $session->get('arr_question_' . $gametype);
	if(!$arr_questions)
	{
		$database->set_query("SELECT id FROM question where question_type_id = $gametype ORDER BY RAND() LIMIT " . $max_question);
		$question_ids = $database->get_object_list();
		$arr_questions = array();
		$index = 1;
		foreach($question_ids as $question_item)
		{
			$arr_questions[$index++] = get_question_info($gametype, $member_id, $question_item->id);
		}
		$session->set('arr_question_' . $gametype,$arr_questions);
	}
	return $arr_questions;
}

function get_question_info($gametype, $member_id, $question_id)
{
	global $database;
	$database->set_query("
		SELECT
		  question.id 	 AS question_id,
		  question.title AS question_text,
		  image 		 AS question_image,
		  answer.id      AS answer_id,
		  answer.content AS answer_text,
		  IF(member_answer_question.answer_id IS NULL,'','selected') selected
		FROM question
		  LEFT JOIN answer
			ON answer.question_id = question.id
		  LEFT JOIN member_answer_question
			ON member_answer_question.answer_id = answer.id
			AND member_id = {$member_id}
		WHERE question_type_id = {$gametype}
			AND question.id = {$question_id}
		ORDER BY RAND()
		LIMIT " . MAX_ANSWER);
	
	$answers = $database->get_object_list();
	$question = array(
		'id'		=> $answers[0]->question_id,
		'text'		=> $answers[0]->question_text,
		'image'		=> $answers[0]->question_image,
		'answers'	=> array(),
	);
	$answer_index = 1;
	foreach($answers as $answer)
	{
		$question['answers'][$answer_index++] = array('id' => $answer->answer_id, 'text' => $answer->answer_text, 'selected' => $answer->selected);
	}
	return $question;
}

function get_remaining_question($member_id, $gametype, $max_question)
{
	global $database;
	$database->set_query("	SELECT COUNT(question_id) 
							FROM member_answer_question 
							INNER JOIN question ON question.id = member_answer_question.question_id
							WHERE question.question_type_id = $gametype AND member_id = $member_id");
	$answered = $database->get_result();
	$remaining = $max_question - $answered;
	return $remaining;
}

function redirect($page)
{
	header("Location: $page");
}

function validEmail($email) {
	if(preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email))
		return true;
	else
		return false;
}

function generatePassword($length = 8)
{
	$password = "";
	$possible = "2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ";

	$maxlength = strlen($possible);
	
	if ($length > $maxlength) {
		$length = $maxlength;
	}

	$i = 0;

	while ($i < $length) {
		$char = substr($possible, mt_rand(0, $maxlength - 1), 1);
		if (!strstr($password, $char)) {
			$password .= $char;
			$i++;
		}
	}
	return $password;
}

function mail_utf8($to, $subject = '(No subject)', $message = '', $header = '') {
  $header_ = 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
  return mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $header_ . $header);
}

?>
